import generator, os, json

all_apps = []

if not os.path.isdir("Bito"):
    print("Error environment! cannot match directory")
    exit(1)

os.chdir("Bito")
for f in os.listdir("apps"):
    if not os.path.isdir(os.path.join("apps", f)):
        continue
    all_apps.append(f)

for app in all_apps:
    app_environ = os.listdir(os.path.join("apps", app))
    if "tags.json" in app_environ:
        with open(os.path.join("apps", app, "tags.json"), 'r', encoding="utf-8") as tags:
            generator.loadImportable(json.loads(tags.read()))
    if "model.json" in app_environ:
        with open(os.path.join("apps", app, "model.json"), 'r', encoding="utf-8") as model:
            generator.public(os.path.join("apps", app, app + ".html"), generator.toJS(generator.parseJson(json.loads(model.read()))))